package com.youlu.campus.service.linker.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.HuoLinkerInfo;
import com.youlu.campus.service.linker.HuoLinkerInfoService;
import com.youlu.campus.service.platform.PlatformConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Service
@Slf4j
public class HuoLinkerInfoServiceImpl implements HuoLinkerInfoService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private PlatformConfigService platformConfigService;

    @Override
    public HuoLinkerInfo find(String key) {
        log.info(":>>> 开始查找短链接:{}", key);
        Query query = new Query().addCriteria(Criteria.where("key").is(key));
        HuoLinkerInfo huoLinkerInfo = mongoTemplate.findOne(query, HuoLinkerInfo.class);
        ActivityInfo activityInfo = null;
        if (StringUtils.isNotBlank(huoLinkerInfo.getActivityId())) {
            activityInfo = this.findActivityInfo(huoLinkerInfo.getActivityId());
        }
        String originDomain = huoLinkerInfo.getDomain();
        String blackListCatalogue=platformConfigService.getBlackListCatalogue();
        log.info(":>>> key:{}，活动类型:{} 业务类型:{} ,活链接为:{}", key, huoLinkerInfo.getActivityType(), huoLinkerInfo.getBizType(), JSON.toJSONString(huoLinkerInfo));
        if (Objects.nonNull(huoLinkerInfo) && StringUtils.isNotBlank(huoLinkerInfo.getBizType())) {

            String domain = huoLinkerInfo.getDomain();
//            业务类型,H:海报,A:活动,P:收款,C:竞赛
            switch (huoLinkerInfo.getBizType()) {
                case "H":
                    if ("qq-h5".equals(huoLinkerInfo.getPlatform())) {
                        domain = platformConfigService.getQQBizFrontDomain();
                    } else {
                        if (!platformConfigService.getBizFrontDomain().equals(huoLinkerInfo.getDomain())) {
                            domain = platformConfigService.getBizFrontDomain();
                        }
                    }
                    break;
                case "A":
                    if (!platformConfigService.getBizFrontDomain().equals(huoLinkerInfo.getDomain())) {
                        domain = platformConfigService.getBizFrontDomain();
                    }
                    break;
                case "P":
                    if (!platformConfigService.getBizFrontPayDomain().equals(huoLinkerInfo.getDomain())) {
                        domain = platformConfigService.getBizFrontPayDomain();
                    }
                    break;
                case "C":
                    if (!platformConfigService.getContestBizFrontDomain().equals(huoLinkerInfo.getDomain())) {
                        domain = platformConfigService.getContestBizFrontDomain();
                    }
                    break;
            }
//            String url = String.format(huoLinkerInfo.getUrl(), domain);
            String url = "";
            if ("C".equals(huoLinkerInfo.getBizType())) {
                url = String.format(huoLinkerInfo.getUrl(), domain);
                url = platformConfigService.replaceActiveCatalogue(url, platformConfigService.getContestCatalogue(), "C");
            } else {
                switch (huoLinkerInfo.getActivityType()) {
                    case "before": {
                        url = huoLinkerInfo.getUrl();
                        if (!url.contains("&appId=") && Objects.nonNull(activityInfo) && "J".equals(huoLinkerInfo.getBizType())) {
                            url = url + "&appId=" + activityInfo.getWechatAppId();
                            log.info(":>>> 金数据类活动:{},拼接appId:{}", activityInfo.getId(), url);
                        }
                        String category = platformConfigService.getCatalogue();
                        if (url.startsWith("%s")) {
                            url = String.format(url, domain);
                        }
                        url = platformConfigService.replaceActiveCatalogue(url, category, "A");
                        url = replaceBlackListCategory(url, blackListCatalogue,
                                category);
                        break;
                    }
                    case "h5++": {
                        url = huoLinkerInfo.getUrl();
                        String category = platformConfigService.getCatalogue();
                        if (url.startsWith("%s")) {
                            url = String.format(url, domain);
                        }
                        url = platformConfigService.replaceActiveCatalogue(url, category, "A");
                        url = replaceBlackListCategory(url, blackListCatalogue,
                                category);
                        break;
                    }
                    case "contest-plus": {
                        url = String.format(huoLinkerInfo.getUrl(), domain);
                        url = platformConfigService.replaceActiveCatalogue(url, platformConfigService.getContestPlusCatalogue(), "C-PLUS");
                        break;
                    }
                    default: {
                        url = huoLinkerInfo.getUrl();
                        String category = platformConfigService.getCatalogue();
                        if (url.startsWith("%s")) {
                            url = String.format(url, domain);
                        }
                        url = platformConfigService.replaceActiveCatalogue(url, category, "A");
                        url = replaceBlackListCategory(url, blackListCatalogue,
                                category);
                        break;
                    }
                }
            }
            if (platformConfigService.changDomain() && url.contains(originDomain)) {
                url.replace(originDomain, domain);
            }

            huoLinkerInfo.setUrl(url);
        }
        return huoLinkerInfo;
    }

    /**
     * 替换黑名单中的目录
     *
     * @param url
     * @param blackListCatalogue
     * @param currentCatalogue
     * @return
     */
    private String replaceBlackListCategory(String url, String blackListCatalogue,
                                            String currentCatalogue) {
        if(StringUtils.isBlank(blackListCatalogue)||Objects.isNull(blackListCatalogue)){
            return url;
        }
        if (url.contains(blackListCatalogue)) {
            url=url.replace(blackListCatalogue, currentCatalogue);
        }
        return url;
    }

    @Override
    public void create(HuoLinkerInfo huoLinkerInfo) {
        log.info(":>>> 开始创建活链接:{}", huoLinkerInfo);
        huoLinkerInfo.setId(null);
        huoLinkerInfo.setCreatedTime(new Date());
        mongoTemplate.insert(huoLinkerInfo);
    }

    @Override
    public boolean exist(String key) {
        Query query = new Query().addCriteria(Criteria.where("key").is(key));
        boolean e = mongoTemplate.exists(query, HuoLinkerInfo.class);
        log.info(":>>> key:{} 是否存在:{}", key, e);
        return e;
    }

    private ActivityInfo findActivityInfo(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, ActivityInfo.class);
    }
}
